#ifndef SINGLETON_H
#define SINGLETON_H
#include "global.h"

template<typename T>
class Singleton
{
   protected:
        Singleton() = default;
        Singleton(const Singleton<T> &sl) = delete;
        Singleton& operator = (const Singleton<T> &sl) = delete;
        static std::shared_ptr<T> instance_;

    public:
        static std::shared_ptr<T> getInstance()
        {
            static std::once_flag s_flag;
            std::call_once(s_flag, [&]() {
                instance_ = std::shared_ptr<T>(new T);
                });
            return instance_;
        }

        void PrintAddress()
        {
            std::cout << instance_.get() << std::endl;
        }
        ~Singleton()
        {
            std::cout << "this is singleton destruct" << std::endl;
        }
};

/*静态成员变量必须在类外部进行初始化，这是因为在类定义中只能声明静态成员变量，而无法直接提供初始值*/
template <typename T>
std::shared_ptr<T> Singleton<T>::instance_ = nullptr;//需要提前初始化类静态成员变量

#endif // SINGLETON_H
